home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 115_01.zip / ED4.C < prev    next >
Text File  |  1993-06-01  |  13KB  |  701 lines

  1. /*
  2.  * Screen editor:  window module
  3.  *
  4.  * Source:  ed4.c
  5.  * Version: December 20, 1981.
  6.  */
  7.  
  8. /* data global to this module */
  9.  
  10. char    editbuf[MAXLEN];    /* the edit buffer */
  11. int    editp;            /* cursor: buffer index */
  12. int    editpmax;        /* length of buffer */
  13. int    edcflag;        /* buffer change flag */
  14.  
  15. /* abort any changes made to current line */
  16.  
  17. edabt()
  18. {
  19.     /* get unchanged line and reset cursor */
  20.     edgetln();
  21.     edredraw();
  22.     edbegin();
  23.     edcflag=NO;
  24. }
  25.  
  26. /* put cursor at beginning of current line */
  27.  
  28. edbegin()
  29. {
  30.     editp=0;
  31.     outxy(0,outgety());
  32. }
  33.  
  34. /* change editbuf[editp] to c
  35.  * don't make change if line would become to long
  36.  */
  37.  
  38. edchng(c) char c;
  39. {
  40. char oldc;
  41. int k;
  42.     /* if at right margin then insert char */
  43.     if (editp>=editpmax) {
  44.         edins(c);
  45.         return;
  46.     }
  47.     /* change char and print length of line */
  48.     oldc=editbuf[editp];
  49.     editbuf[editp]=c;
  50.     fmtadj(editbuf,editp,editpmax);
  51.     k=fmtlen(editbuf,editpmax);
  52.     if (k>SCRNW1) {
  53.         /* line would become too long */
  54.         /* undo the change */
  55.         editbuf[editp]=oldc;
  56.         fmtadj(editbuf,editp,editpmax);
  57.     }
  58.     else {
  59.         /* set change flag, redraw line */
  60.         edcflag=YES;
  61.         editp++;
  62.         edredraw();
  63.     }
  64. }
  65.  
  66. /* delete the char to left of cursor if it exists */
  67.  
  68. eddel()
  69. {
  70. int k;
  71.     /* just move left one column if past end of line */
  72.     if (edxpos() < outgetx()) {
  73.         outxy(outgetx()-1, outgety());
  74.         return;
  75.     }
  76.     /* do nothing if cursor is at left margin */
  77.     if (editp==0) {
  78.         return;
  79.     }
  80.     edcflag=YES;
  81.     /* compress buffer (delete char) */
  82.     k=editp;
  83.     while (k<editpmax) {
  84.         editbuf[k-1]=editbuf[k];
  85.         k++;
  86.     }
  87.     /* update pointers, redraw line */
  88.     editp--;
  89.     editpmax--;
  90.     edredraw();
  91. }
  92.  
  93. /* edit the next line.  do not go to end of buffer */
  94.  
  95. eddn()
  96. {
  97. int oldx;
  98.     /* save visual position of cursor */
  99.     oldx=outgetx();
  100.     /* replace current edit line */
  101.     if (edrepl()!=OK) {
  102.         return(ERR);
  103.     }
  104.     /* do not go past last non-null line */
  105.     if (bufnrbot()) {
  106.         return(OK);
  107.     }
  108.     /* move down one line in buffer */
  109.     if (bufdn()!=OK) {
  110.         return(ERR);
  111.     }
  112.     edgetln();
  113.     /* put cursor as close as possible on this
  114.      * new line to where it was on the old line.
  115.      */
  116.     editp=edscan(oldx);
  117.     /* update screen */
  118.     if (edatbot()) {
  119.         edsup(bufln()-SCRNL2);
  120.         outxy(oldx, SCRNL1);
  121.     }
  122.     else {
  123.         outxy(oldx, outgety()+1);
  124.     }
  125.     return(OK);
  126. }
  127.  
  128. /* put cursor at the end of the current line */
  129.  
  130. edend()
  131. {
  132.     editp=editpmax;
  133.     outxy(edxpos(),outgety());
  134.     
  135.     /* comment out ----- put cursor at end of screen
  136.     outxy(SCRNW1, outgety());
  137.     ----- end comment out */
  138. }
  139.  
  140. /* start editing line n
  141.  * redraw the screen with cursor at position p
  142.  */
  143.  
  144. edgo(n, p) int n, p;
  145. {
  146.     /* replace current line */
  147.     if (edrepl()==ERR) {
  148.         return(ERR);
  149.     }
  150.     /* go to new line */
  151.     if (bufgo(n)==ERR) {
  152.         return(ERR);
  153.     }
  154.     /* prevent going past end of buffer */
  155.     if (bufatbot()) {
  156.         if (bufup()==ERR) {
  157.             return(ERR);
  158.         }
  159.     }
  160.     /* redraw the screen */
  161.     bufout(bufln(),1,SCRNL1);
  162.     edgetln();
  163.     editp=min(p, editpmax);
  164.     outxy(edxpos(), 1);
  165.     return(OK);
  166. }
  167.  
  168. /* insert c into the buffer if possible */
  169.  
  170. edins(c) char c;
  171. {
  172. int k;
  173.  
  174.     /* do nothing if edit buffer is full */
  175.     if (editpmax>=MAXLEN) {
  176.         return;
  177.     }
  178.     /* fill out line if we are past its end */
  179.     if ((editp == editpmax) & (edxpos() < outgetx())) {
  180.         k = outgetx() - edxpos();
  181.         editpmax = editpmax + k;
  182.         while (k-- > 0) {
  183.             editbuf [editp++] = ' ';
  184.         }
  185.         editp = editpmax;
  186.     }
  187.     /* make room for inserted character */
  188.     k=editpmax;
  189.     while (k>editp) {
  190.         editbuf[k]=editbuf[k-1];
  191.         k--;
  192.     }
  193.     /* insert character. update pointers */
  194.     editbuf[editp]=c;
  195.     editp++;
  196.     editpmax++;
  197.     /* recalculate print length of line  */
  198.     fmtadj(editbuf,editp-1,editpmax);
  199.     k=fmtlen(editbuf,editpmax);
  200.     if (k>SCRNW1) {
  201.         /* line would become too long */
  202.         /* delete what we just inserted */
  203.         eddel();
  204.     }
  205.     else {
  206.         /* set change flag, redraw line */
  207.         edcflag=YES;
  208.         edredraw();
  209.     }
  210. }
  211.  
  212. /* join (concatenate) the current line with the one above it */
  213.  
  214. edjoin()
  215. {
  216. int k;
  217.  
  218.     /* do nothing if at top of file */
  219.     if (bufattop()) {
  220.         return;
  221.     }
  222.     /* replace lower line temporarily */
  223.     if (edrepl() != OK) {
  224.         return;
  225.     }
  226.     /* get upper line into buffer */
  227.     if (bufup() != OK) {
  228.         return;
  229.     }
  230.     k = bufgetln(editbuf, MAXLEN);
  231.     /* append lower line to buffer */
  232.     if (bufdn() != OK) {
  233.         return;
  234.     }
  235.     k = k + bufgetln(editbuf+k, MAXLEN-k);
  236.     /* abort if the screen isn't wide enough */
  237.     if (k>SCRNW1) {
  238.         /* bug fix */
  239.         bufgetln(editbuf, MAXLEN);
  240.         return;
  241.     }
  242.     /* replace upper line */
  243.     if (bufup() != OK) {
  244.         return;
  245.     }
  246.     editpmax = k;
  247.     edcflag = YES;
  248.     if (edrepl() != OK) {
  249.         return;
  250.     }
  251.     /* delete the lower line */
  252.     if (bufdn() != OK) {
  253.         return;
  254.     }
  255.     if (bufdel() != OK) {
  256.         return;
  257.     }
  258.     if (bufup() != OK) {
  259.         return;
  260.     }
  261.     /* update the screen */
  262.     if (edattop()) {
  263.          edredraw();
  264.     }
  265.     else {
  266.         k = outgety() - 1;
  267.         bufout(bufln(),k,SCRNL-k);
  268.         outxy(0,k);
  269.         edredraw();
  270.     }
  271. }
  272.  
  273. /* delete chars until end of line or c found */
  274.  
  275. edkill(c) char c;
  276. {
  277. int k,p;
  278.     /* do nothing if at right margin */
  279.     if (editp==editpmax) {
  280.         return;
  281.     }
  282.     edcflag=YES;
  283.     /* count number of deleted chars */
  284.     k=1;
  285.     while ((editp+k)<editpmax) {
  286.         if (editbuf[editp+k]==c) {
  287.             break;
  288.         }
  289.         else {
  290.             k++;
  291.         }
  292.     }
  293.     /* compress buffer (delete chars) */
  294.     p=editp+k;
  295.     while (p<editpmax) {
  296.         editbuf[p-k]=editbuf[p];
  297.         p++;
  298.     }
  299.     /* update buffer size, redraw line */
  300.     editpmax=editpmax-k;
  301.     edredraw();
  302. }
  303.  
  304. /* move cursor left one column.
  305.  * never move the cursor off the current line.
  306.  */
  307.  
  308. edleft()
  309. {
  310. int k;
  311.  
  312.     /* if past right margin, move left one column */
  313.     if (edxpos() < outgetx()) {
  314.         outxy(max(0, outgetx()-1), outgety());
  315.     }
  316.     /* inside the line.  move left one character */
  317.     else if (editp!=0) {
  318.         editp--;
  319.         outxy(edxpos(),outgety());
  320.     }
  321. }
  322.  
  323. /* insert a new blank line below the current line */
  324.  
  325. ednewdn()
  326. {
  327. int k;
  328.     /* make sure there is a current line and 
  329.      * put the current line back into the buffer.
  330.      */
  331.     if (bufatbot()) {
  332.         if (bufins(editbuf,editpmax)!=OK) {
  333.             return;
  334.         }
  335.     }
  336.     else if (edrepl()!=OK) {
  337.             return;
  338.     }
  339.     /* move past current line */
  340.     if (bufdn()!=OK) {
  341.         return;
  342.     }
  343.     /* insert place holder:  zero length line */
  344.     if (bufins(editbuf,0)!=OK) {
  345.         return;
  346.     }
  347.     /* start editing the zero length line */
  348.     edgetln();
  349.     /* update the screen */
  350.     if (edatbot()) {
  351.         /* note: bufln() >= SCRNL */
  352.         edsup(bufln()-SCRNL2);
  353.         outxy(edxpos(),SCRNL1);
  354.     }
  355.     else {
  356.         k=outgety();
  357.         bufout(bufln(),k+1,SCRNL1-k);
  358.         outxy(edxpos(),k+1);
  359.     }
  360. }
  361.  
  362. /* insert a new blank line above the current line */
  363.  
  364. ednewup()
  365. {
  366. int k;
  367.     /* put current line back in buffer */
  368.     if (edrepl()!=OK) {
  369.         return;
  370.     }
  371.     /* insert zero length line at current line */
  372.     if (bufins(editbuf,0)!=OK) {
  373.         return;
  374.     }
  375.     /* start editing the zero length line */
  376.     edgetln();
  377.     /* update the screen */
  378.     if (edattop()) {
  379.         edsdn(bufln());
  380.         outxy(edxpos(),1);
  381.     }
  382.     else {
  383.         k=outgety();
  384.         bufout(bufln(),k,SCRNL-k);
  385.         outxy(edxpos(),k);
  386.     }
  387. }
  388.  
  389. /* move cursor right one character.
  390.  * never move the cursor off the current line.
  391.  */
  392.  
  393. edright()
  394. {
  395.     /* if we are outside the line move right one column */
  396.     if (edxpos() < outgetx()) {
  397.         outxy (min(SCRNW1, outgetx()+1), outgety());
  398.     }
  399.     /* if we are inside a tab move to the end of it */
  400.     else if (edxpos() > outgetx()) {
  401.         outxy (edxpos(), outgety());
  402.     }
  403.     /* move right one character if inside line */
  404.     else if (editp < editpmax) {
  405.         editp++;
  406.         outxy(edxpos(),outgety());
  407.     }
  408.     /* else move past end of line */
  409.     else {
  410.         outxy (min(SCRNW1, outgetx()+1), outgety());
  411.     }
  412. }
  413.  
  414. /* split the current line into two parts.
  415.  * scroll the first half of the old line up.
  416.  */
  417.  
  418. edsplit()
  419. {
  420. int p, q;
  421. int k;
  422.  
  423.     /* indicate that edit buffer has been saved */
  424.     edcflag = NO;
  425.     /* replace current line by the first half of line */
  426.     if (bufatbot()) {
  427.         if (bufins(editbuf, editp) != OK) {
  428.             return;
  429.         }
  430.     }
  431.     else {
  432.         if (bufrepl(editbuf, editp) != OK) {
  433.             return;
  434.         }
  435.     }
  436.     /* redraw the first half of the line */
  437.     p = editpmax;
  438.     q = editp;
  439.     editpmax = editp;
  440.     editp = 0;
  441.     edredraw();
  442.     /* move the second half of the line down */
  443.     editp = 0;
  444.     while (q < p) {
  445.         editbuf [editp++] = editbuf [q++];
  446.     }
  447.     editpmax = editp;
  448.     editp = 0;
  449.     /* insert second half of the line below the first */
  450.     if (bufdn() != OK) {
  451.         return;
  452.     }
  453.     if (bufins(editbuf, editpmax) != OK) {
  454.         return;
  455.     }
  456.     /* scroll the screen up and draw the second half */
  457.     if (edatbot()) {
  458.         edsup(bufln()-SCRNL2);
  459.         outxy(1,SCRNL1);
  460.         edredraw();
  461.     }
  462.     else {
  463.         k = outgety();
  464.         bufout(bufln(), k+1, SCRNL1-k);
  465.         outxy(1, k+1);
  466.         edredraw();
  467.     }
  468. }
  469.  
  470. /* move cursor right until end of line or
  471.  * character c found.
  472.  */
  473.  
  474. edsrch(c) char c;
  475. {
  476.     /* do nothing if at right margin */
  477.     if (editp==editpmax) {
  478.         return;
  479.     }
  480.     /* scan for search character */
  481.     editp++;
  482.     while (editp<editpmax) {
  483.         if (editbuf[editp]==c) {
  484.             break;
  485.         }
  486.         else {
  487.             editp++;
  488.         }
  489.     }
  490.     /* reset cursor */
  491.     outxy(edxpos(),outgety());
  492. }
  493.  
  494. /* move cursor up one line if possible */
  495.  
  496. edup()
  497. {
  498. int oldx;
  499.     /* save visual position of cursor */
  500.     oldx=outgetx();
  501.     /* put current line back in buffer */
  502.     if (edrepl()!=OK) {
  503.         return(ERR);
  504.     }
  505.     /* done if at top of buffer */
  506.     if (bufattop()) {
  507.         return(OK);
  508.     }
  509.     /* start editing the previous line */
  510.     if (bufup()!=OK) {
  511.         return(ERR);
  512.     }
  513.     edgetln();
  514.     /* put cursor on this new line as close as
  515.      * possible to where it was on the old line.
  516.      */
  517.     editp=edscan(oldx);
  518.     /* update screen */
  519.     if (edattop()) {
  520.         edsdn(bufln());
  521.         outxy(oldx, 1);
  522.     }
  523.     else {
  524.         outxy(oldx, outgety()-1);
  525.     }
  526.     return(OK);
  527. }
  528.  
  529. /* delete the current line */
  530.  
  531. edzap()
  532. {
  533. int k;
  534.     /* delete the line in the buffer */
  535.     if (bufdel()!=OK) {
  536.         return;
  537.     }
  538.     /* move up one line if now at bottom */
  539.     if (bufatbot()) {
  540.         if (bufup()!=OK) {
  541.             return;
  542.         }
  543.         edgetln();
  544.         /* update screen */
  545.         if (edattop()) {
  546.             edredraw();
  547.         }
  548.         else {
  549.             outdelln();
  550.             outxy(0,outgety()-1);
  551.         }
  552.         return;
  553.     }
  554.     /* start editing new line */
  555.     edgetln();
  556.     /* update screen */
  557.     if (edattop()) {
  558.         edsup(bufln());
  559.         outxy(0,1);
  560.     }
  561.     else {
  562.         k=outgety();
  563.         bufout(bufln(),k,SCRNL-k);
  564.         outxy(0,k);
  565.     }
  566. }
  567.  
  568. /* return true if the current edit line is being
  569.  * displayed on the bottom line of the screen.
  570.  */
  571.  
  572. edatbot()
  573. {
  574.     return(outgety()==SCRNL1);
  575. }
  576.  
  577. /* return true if the current edit line is being
  578.  * displayed on the bottom line of the screen.
  579.  */
  580.  
  581. edattop()
  582. {
  583.     return(outgety()==1);
  584. }
  585.  
  586. /* redraw edit line from index to end of line */
  587. /* reposition cursor */
  588.  
  589. edredraw()
  590. {
  591.     fmtadj(editbuf,0,editpmax);
  592.     fmtsubs(editbuf,max(0,editp-1),editpmax);
  593.     outxy(edxpos(),outgety());
  594. }
  595.  
  596. /* return the x position of the cursor on screen */
  597.  
  598. edxpos()
  599. {
  600.     return(min(SCRNW1,fmtlen(editbuf,editp)));
  601. }
  602.  
  603.  
  604. /* fill edit buffer from current main buffer line.
  605.  * the caller must check to make sure the main
  606.  * buffer is available.
  607.  */
  608.  
  609. edgetln()
  610. {
  611. int k;
  612.     /* put cursor on left margin, reset flag */
  613.     editp=0;
  614.     edcflag=NO;
  615.     /* get edit line from main buffer */
  616.     k=bufgetln(editbuf,MAXLEN);
  617.     if (k>MAXLEN) {
  618.         error("line truncated");
  619.         editpmax=MAXLEN;
  620.     }
  621.     else {
  622.         editpmax=k;
  623.     }
  624.     fmtadj(editbuf,0,editpmax);
  625. }
  626.  
  627. /* replace current main buffer line by edit buffer.
  628.  * the edit buffer is NOT changed or cleared.
  629.  * return ERR is something goes wrong.
  630.  */
  631.  
  632. edrepl()
  633. {
  634.     /* do nothing if nothing has changed */
  635.     if (edcflag==NO) {
  636.         return(OK);
  637.     }
  638.     /* make sure we don't replace the line twice */
  639.     edcflag=NO;
  640.     /* insert instead of replace if at bottom of file */
  641.     if (bufatbot()) {
  642.         return(bufins(editbuf,editpmax));
  643.     }
  644.     else {
  645.         return(bufrepl(editbuf,editpmax));
  646.     }
  647. }
  648.  
  649. /* set editp to the largest index such that
  650.  * buf[editp] will be printed <= xpos
  651.  */
  652.  
  653. edscan(xpos) int xpos;
  654. {
  655.     editp=0;
  656.     while (editp<editpmax) {
  657.         if (fmtlen(editbuf,editp)<xpos) {
  658.             editp++;
  659.         }
  660.         else {
  661.             break;
  662.         }
  663.     }
  664.     return(editp);
  665. }
  666.  
  667. /* scroll the screen up.  topline will be new top line */
  668.  
  669. edsup(topline) int topline;
  670. {
  671.     if (outhasup()==YES) {
  672.         /* hardware scroll */
  673.         outsup();
  674.         /* redraw bottom line */
  675.         bufout(topline+SCRNL2,SCRNL1,1);
  676.     }
  677.     else {
  678.         /* redraw whole screen */
  679.         bufout(topline,1,SCRNL1);
  680.     }
  681. }
  682.  
  683. /* scroll screen down.  topline will be new top line */
  684.  
  685. edsdn(topline) int topline;
  686. {
  687.     if (outhasdn()==YES) {
  688.         /* hardware scroll */
  689.         outsdn();
  690.         /* redraw top line */
  691.         bufout(topline,1,1);
  692.     }
  693.     else {
  694.         /* redraw whole screen */
  695.         bufout(topline,1,SCRNL1);
  696.     }
  697. }
  698. g=NO;
  699.     /* insert instead of replace if at bottom of file */
  700.     if (bufatbot()) {